package com.coin900.coin.service.fu;

import com.coin900.coin.base.FuConstants;
import com.coin900.coin.persist.fu.FuUserAuthVO;
import com.coin900.coin.persist.fu.FuUserVO;
import com.value.mybatis.IDaoRouter;
import com.value.mybatis.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author shui
 * @create 2017-11-21
 **/
@Service
public class FuUserAuthService implements IFuUserAuthService {

    @Override
    public List<FuUserAuthVO> loadFuUserAuth(Map<String, Object> params) {
        return this.daoRouter.query("FuUserAuth.loadFuUserAuth", params);
    }

    @Override
    public List<FuUserAuthVO> loadFuUserAuthForPage(Pagination page) {
        return this.daoRouter.query("FuUserAuth.loadFuUserAuthForPage", page);
    }

    @Override
    public void insertFuUserAuth(FuUserAuthVO authVO) {
        this.daoRouter.insert("FuUserAuth.insertFuUserAuth", authVO);
        // 将用户的实名认证状态改成认证中
        FuUserVO user = new FuUserVO();
        user.setId(authVO.getUserId());
        user.setIdcardStatus(FuConstants.FU_CHECK_STATUS_ING);
        this.userService.updateFuUser(user);
    }

    @Override
    public void updateFuUserAuth(FuUserAuthVO authVO) {
        this.daoRouter.update("FuUserAuth.updateFuUserAuth", authVO);
    }

    @Override
    public void checkFuUserAuth(FuUserAuthVO userAuthVO) {
        this.daoRouter.update("FuUserAuth.updateFuUserAuth", userAuthVO);
        Integer checkStatus = userAuthVO.getCheckStatus();
        FuUserVO userVO = new FuUserVO();
        userVO.setId(userAuthVO.getUserId());
        switch (checkStatus) {
            case FuConstants.FU_CHECK_STATUS_SUCCESS:
                userVO.setIdcardStatus(FuConstants.FU_CHECK_STATUS_SUCCESS);
                break;
            case FuConstants.FU_CHECK_STATUS_FAIL:
                userVO.setIdcardStatus(3);
                // 认证失败删除认证记录
                this.deleteFuUserAuth(userAuthVO);
                break;
            default:
                break;
        }
        this.userService.updateFuUser(userVO);
    }

    @Override
    public void deleteFuUserAuth(FuUserAuthVO vo) {
        this.daoRouter.update("FuUserAuth.deleteFuUserAuth", vo);
    }

    @Autowired
    private IFuUserService userService;
    @Autowired
    private IDaoRouter daoRouter;
}